import http from "@/plugins/axios"

// 管理员登录
export function adminLogin(data: LoginForm) {
    return http.post<LoginResponse | ValidateError>({
        url: '/admin/login',
        data
    })
}

// 管理员退出
export function logoutAdmin() {
    return http.get<null>({ url: '/admin/logout' })
}
// 获取日志记录
export function getAdminLogs(data: AdminLogForm) {
    let d: Record<string, any> = {}
    if (data.admin_id) d.admin_id = data.admin_id
    if (data.router) d.router = data.router
    if (data.time && data.time[0]) d.time1 = data.time[0]
    if (data.time && data.time[1]) d.time2 = data.time[1]
    d.page = data.page
    d.limit = data.limit
    return http.get<PageResponse<AdminLogsModel>>({
        url: '/admin/logs',
        params: { ...d }
    })
}

// 设置系统信息
export function setSystemInfo(data: SystemModel) {
    return http.post<SystemModel>({
        url: '/admin/system',
        data
    })
}

// 上传系统logo
export function updateSystemLogo(data: FormData) {
    return http.post<UploadResponse>({
        url: '/admin/logo',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data
    })
}

// 提交假期信息
export function updateHoliday(data: HolidayModel) {
    return http.post<HolidayModel>({
        url: '/admin/holidays',
        data
    })
}

// 获取分类数据
export function getAssorts() {
    return http.get<AssortModel[]>({ url: '/admin/assort' })
}
// 修改分类数据
export function updateAssort(data: AssortModel) {
    return http.put<AssortModel>({
        url: `/admin/assort/${data.id}`,
        data
    })
}

// 删除分类数据
export function deleteAssort(id: number) {
    return http.delete<null>({ url: `/admin/assort/${id}` })
}
// 新建分类数据
export function createAssort(data: AssortModel) {
    return http.post<AssortModel>({
        url: '/admin/assort',
        data
    })
}

// 获取导航数据
export function getNavigations(data: GetNavRequest) {
    let query = ''
    for (const [key, value] of Object.entries(data)) {
        if (value) {
            query += `&${key}=${value}`
        }
    }
    return http.get<PageResponse<NavigationModel>>({ url: '/admin/navigation?' + query.slice(1) })
}
// 修改导航数据
export function updateNavigation(data: NavigationModel) {
    return http.put<NavigationModel>({
        url: `/admin/navigation/${data.id}`,
        data
    })
}

// 删除导航数据
export function deleteNavigation(id: number) {
    return http.delete<null>({ url: `/admin/navigation/${id}` })
}
// 新建导航数据
export function createNavigation(data: NavigationModel) {
    return http.post<NavigationModel>({
        url: '/admin/navigation',
        data
    })
}

// 获取控制台信息
export function getInfos() {
    return http.get<InfoResponse>({ url: '/admin/index_info' })
}

// 获取最近一月访问量信息
export function getVisitInfo() {
    return http.get<VisitInfo[]>({ url: '/admin/visits' })
}
